package DML;

import java.util.ArrayList;
import java.util.List;

import DuiXiang.TableModel;

public class SelectTableParsing {
	public static int selectTableParsing(String sql,
			List<List<String>> resultSet) {
		if (sql.charAt(sql.length() - 1) != ';') {
			System.out.println("1");
			return -4; // 语法错误
		}

		int i, j;
		boolean flag = true;
		boolean all = false;
		List<String> name = new ArrayList<String>();
		List<String> values = new ArrayList<String>();
		List<String> needName = new ArrayList<String>();
		String[] sqlsplit = sql.split("\\s+");

		String dataBaseName = sqlsplit[sqlsplit.length - 1].substring(0,
				sqlsplit[sqlsplit.length - 1].length() - 1);
		String tableName = sqlsplit[3];
		String dbfPath = "D:\\database\\" + dataBaseName + "\\" + tableName
				+ ".dbf";
		List<TableModel> tableModelList = util.ReadTableModelList(dbfPath);
		if (tableModelList == null){
			System.out.println(tableName);
			return -1; // 无此表或数据库
		}
		if (!sqlsplit[2].equals("FROM") && !sqlsplit[2].equals("from")) {
			System.out.println("2" + " " + sqlsplit[2]);

			return -4; // 语法错误
		}
		if (!sqlsplit[sqlsplit.length - 2].equals("IN")
				&& !sqlsplit[sqlsplit.length - 2].equals("in")) {
			System.out.println("4");
			return -4; // 语法错误
		}
		if (sqlsplit[1].equals("*")) {
			j = 0;
			for (TableModel tt : tableModelList)
				needName.add(tt.getsFieldName());
		} else {
			String[] needName1 = sqlsplit[1].split(",");
			for (String n : needName1) {
				for (i = 0; i < tableModelList.size(); i++) {
					if (tableModelList.get(i).getsFieldName().equals(n))
						break;
				}
				if (i == tableModelList.size())
					return -2;
			}
		}
		if(sqlsplit.length==6)
			all=true;
		else{
		if (!sqlsplit[4].equals("WHERE") && !sqlsplit[4].equals("where")) {
			System.out.println("3");
			return -4; // 语法错误
		}
		for (i = 5; i < (sqlsplit.length - 2); i++) {
			String[] temp = sqlsplit[i].split("=");
			if (temp.length != 2) {
				System.out.println("5");
				return -4; // 语法错误
			}
			for (j = 0; j < tableModelList.size(); j++) {
				if (tableModelList.get(j).getsFieldName().equals(temp[0]))
					break;
			}
			if (j == tableModelList.size())
				return -2;
			for (TableModel tt : tableModelList)
				if ((tt.getsFieldName()).equals(temp[0])) {
					switch (tt.getsType()) {
					case "int":
					case "long":
						for (j = 0; j < temp[1].length(); j++)
							if (temp[1].charAt(j) < '0'
									|| temp[1].charAt(j) > '9')
								break;
						if (j < temp[1].length())
							return -3; // 格式不匹配
						break;
					case "float":
						for (j = 0; j < temp[1].length(); j++) {
							if (temp[1].charAt(j) >= '0'
									&& temp[1].charAt(j) <= '9')
								continue;
							else if (temp[1].charAt(j) == '.' && flag) {
								flag = false;
								continue;
							} else
								break;
						}
						if (j < temp[1].length())
							return -3; // 格式不匹配
						break;
					case "char":
					case "varchar":
						if (temp[1].charAt(0) != '\''
								|| temp[1].charAt(temp[1].length() - 1) != '\'')
							return -3; // 格式不匹配
						else
							temp[1] = temp[1]
									.substring(1, temp[1].length() - 1);
						break;
					default:
						break;
					}
					break;
				}
			name.add(temp[0]);
			values.add(temp[1]);
			if (i + 1 < (sqlsplit.length - 2)) {
				i++;
				if (!sqlsplit[i].equals("AND") && !sqlsplit[i].equals("and")) {
					
					System.out.println(sqlsplit[i]+"6");
					return -4; // 语法错误
				}
			}
		}
		}

		int result = SelectTable.selectDate(dataBaseName, tableName, name,
				values, needName, resultSet ,all); // 得到的查询结果，本来该返回的，先暂时这样吧

		return result;
	}
}
